package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.CarModel;
import com.zhentao.service.CarModelService;
import com.zhentao.util.Result;
import com.zhentao.vo.CarModelVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车型管理控制器
 */
@RestController
@RequestMapping("/car/model")
public class CarModelController {

    @Resource
    private CarModelService carModelService;

    /**
     * 获取车型详情（包含品牌信息）
     */
    @RequestMapping("/getById")
    public Result getModelDetail(@PathVariable Long id) {
        CarModelVO model = carModelService.getModelWithBrand(id);
        return Result.OK(model);
    }

    /**
     * 分页查询车型列表（包含品牌信息）
     */
    @RequestMapping("/page")
    public Result pageModels(@RequestBody com.zhentao.pojo.Page page) {
        Page<CarModel> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<CarModel> queryWrapper = new QueryWrapper<>();
        Page<CarModel> result = carModelService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    /**
     * 获取指定品牌下的所有车型
     */
    @RequestMapping("/brand/{brandId}")
    public Result getModelsByBrand(@PathVariable Long brandId) {
        List<CarModelVO> models = carModelService.listModelsByBrandId(brandId);
        return Result.OK(models);
    }

    /**
     * 添加车型
     */
    @RequestMapping("/add")
    public Result addModel(@RequestBody CarModel carModel) {
        carModelService.save(carModel);
        return Result.OK(carModel);
    }

    /**
     * 更新车型
     */
    @RequestMapping("/upd")
    public Result updateModel(@RequestBody CarModel carModel) {
        return Result.OK(carModelService.updateById(carModel));
    }

    /**
     * 删除车型
     */
    @RequestMapping("/delete")
    public Result deleteModel(@PathVariable Long id) {
        return Result.OK(carModelService.removeById(id));
    }
}
